<?php

namespace App\Service\Moment;

use App\Constants\ErrorCode;
use App\Model\Moment;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 用户的动态列表
 */
class UserMomentListService extends BaseService
{
    public function handle()
    {
        //用户的ID
        $profileUserId = $this->request->input('profile_user_id', 0);
        $page = $this->request->input('page', 1);
        $userId = $this->getUserId();
        $profileUserId = $profileUserId ?: $userId;
        $ret['list'] = $this->packData($profileUserId, $userId, $page);
        return $ret;
    }

    /**
     * 获取数据
     */
    function packData($profileUserId = 0, $userId, $page = 1)
    {
        $list = Moment::getUserList($profileUserId, $page, $this->pageNum);
        $profile = UserProfile::getProfile($profileUserId);
        $data = [];
        array_walk($list, function ($item) use (&$data, $userId, $profile) {
            $data[] = Moment::buildItem($item, $profile, $userId);
        });
        return $data;
    }
}
